import time

import pymysql

def connect_to_database():
    try:
        # 建立数据库连接
        connection = pymysql.connect(
            host='rm-m5et107cgezjbovr5.mysql.rds.aliyuncs.com',
            user='dev_dba',
            password='Cc123dev!@#dbs',
            database='sample_helper_to_b',
            cursorclass=pymysql.cursors.DictCursor
        )
        print("成功连接到数据库！")
        return connection
    except pymysql.Error as e:
        print(f"连接数据库时出错: {e}")
        return None


if __name__ == "__main__":
    conn = connect_to_database()
    user_massage = []

    if conn:
        try:
            with conn.cursor() as cursor:
                # 执行 SQL 查询
                sql = "SELECT * from sys_user"
                # 执行查询
                cursor.execute(sql)
                # 获取查询结果
                result = cursor.fetchall()
                # 打印查询结果
                # print(result)
                for row in result:
                    # print(row)
                    for key, value in row.items():
                        print(f"{key}:{value}，时间：{time.struct_time}")

        except pymysql.Error as e:
            print(f"执行 SQL 查询时出错: {e}")
        finally:
            # 关闭数据库连接
            conn.close()